(
	function ($){
		 $.fn.page = function(options){
			 var defaults = {
				url:"machineList",				//分页请求的url
				type:"post",					//提交类型
				page:".page",					//分页父元素
				body:"",						//分页列表父元素
				template:"#template",			//模板的id
				search:".search",				//自定义点击触发分页的元素
				query:"#query",					//查询条件的from
				allselect:"#allselect",			//全选
				oneselect:"input[name=macs]",	//多选框
				pageTage:"div",					//分页元素
				change:true,					//筛选条件是否改变就请求数据，true改变就请求
				before:function(){return true;},//查询数据之前执行，返回false则不执行分页
				after:function(){},				//生成列表之后执行
				allselected:function(){},		//全选触发事件
				selected:function(){},			//多选框选中之后
				uncheck:function(){},			//多选框取消选择之后	
				upanddown:true,					//是否显示上一页，下一页，首页，尾页
				size:10,						//分页每一页显示的信息的条数
				showPageCount:10,				//显示分页最多显示最大页数
				currpage:1,						//当前的页数
				custom:{						//自定义处理数据的方法
				}
            };
			 defaultsCallback = {						//自定义处理数据的方法
						ifNullToEmpty:function(val){
							return val || "";
						},
						ifNullToZero:function(val){
							return val || 0;
						},
						ifNullToNothing:function(val){
							return val || "无";
						},
						ifNullToNotDefined:function(val){
							return val || "未定义";
						}
					}
			//加载默认函数
			options.custom = $.extend(defaultsCallback, options.custom);
			options = $.extend(defaults, options);

			function init(obj){
				options.warp =  $(obj);
	            options.body = options.body=="" ? options.warp : options.warp.find(options.body);
	            options.templateText = $(options.template).html();
	            options.query = $(options.query);
	            options.page = options.warp.find(options.page);
	            options.allselect = $(options.allselect);
	            options.search = $(options.search);
	            
	            //主动请求数据
	            options.search.click(pageList);
	            
	            //分页
	            options.page.on("click",".em",function(){
	           			pageList(this);
	           	});
	            	
            	//跳转
	            options.page.on("click",".jump",function(){
	            		$(this).attr("data",$(this).prev().val());
	           			pageList(this);
	           	});

		        //全选
	            options.allselect.click(function(){
		        	var checked = this.checked;
		        	$.each($(options.oneselect),function(){
		        		this.checked = checked;
		        	});
		        	//添加设备
		        	options.allselected();
		        });
		
		        //单个取消时取消全选按钮
	            options.body.on("click",options.oneselect,function(){
		        	if(!this.checked){
		        		$(allselect).attr("checked",this.checked);
		        	}
		        	if(this.checked){
		        		options.selected(this);
		        	}else{
		        		options.uncheck(this);
		        	}
		        });
		        
		        options.query.find("[name]").on("change",function(){
		        	if(options.change == true){
			        	pageList();
		        	}
		        })
		        
		        pageList();
			}
			
			//把表单装换成对象
			function serializeObject(){
		           if(options.query.length>1){
		              return false;
		           }
		           var arr= options.query.serializeArray();
		           var obj=new Object;
		           $.each(arr,function(k,v){
		        	   if(v.value && v.value!=""){
		        		   obj[v.name]=v.value;
		        	   }
		           });
		           return obj;
		       }
			
			//请求分页数据
           	function pageList(obj){
	          	if(options.before()){
	          		if($(obj).attr("data") != "no"){
	          		var postdata = serializeObject();
			        postdata["rows"]=options.size;
	          		postdata["page"]= $(obj).attr("data") ? $(obj).attr("data") : options.currpage;
	          		$.ajax({
	          			url:options.url,
	          			type:options.type,
	          			data:postdata,
	          			dataType:"json",
	          			success:function(data){
	          				showList(data.rows,postdata["page"]);
	          				paging(data.total,postdata["page"]);
	          			}
	          		});
	          		}
	          	}
           	}
			
			//获取元素
          	function getVal(obj,emelent){
          		var ems =  emelent.split(",");
          		if(ems.length==1){
          			var em = ems[0].replace(/\s+/g,"").split(".");
	        		for(var i=0; i<em.length; i++){
	        			obj = em[i] in obj?obj[em[i]]:"";
	        		}
          		}else if(ems.length>1){
          			var retDate = [];
          			for(var i= 0 ; i<ems.length; i++){
          				var tmp = obj;
	          			var em = ems[i].replace(/\s+/g,"").split(".");
		        		for(var n=0; n<em.length; n++){
		        			tmp = tmp[em[n]];
		        		}
		        		retDate[i]=tmp;
	          		}
          			obj = retDate;
          		}
        		return obj;
        	}
          	
          //生成列表
	          function showList(rows,currpage){
	        	currpage = getCurrpage(currpage);
	          	options.body.html("");
	          	var templateHtml = "";
	          	$.each(rows,function(i,row){
	          		var replaceRegExp = /\{\{(.+?)\}\}/g, functionRegExp = /(\w+)\((.+)\)/;
	          		templateHtml += options.templateText.replace(replaceRegExp,
	          					function(a,b){
									var matchArr = b.match(functionRegExp);
									if(matchArr){
										return options.custom[matchArr[1]](getVal(row,matchArr[2]));
									}else{
										
										return getVal(row,b);
									}
								});
	          		
	          	});
	          	options.body.html(templateHtml);
	          }
	          
	       function getCurrpage(currpage){
	    	   try{
	          		currpage = parseInt(currpage);
	          		currpage = currpage < 1 ? 1 : currpage;
	          	}catch (e) {
	          		currpage = 1;
	          	}
	          	return currpage;
	       }  
	        
          	function paging(total,currpage){
          		currpage = getCurrpage(currpage);
          		options.page.html("");
          		if(options.upanddown){
		          	if( currpage > 1){
		          		options.page.append("<"+options.pageTage+" class='em activ' data='1'>首页</"+options.pageTage+">");
		          		options.page.append("<"+options.pageTage+" class='em activ' data='"+(currpage-1)+"'>上一页</"+options.pageTage+">");
		          	}else{
		          		options.page.append("<"+options.pageTage+" class='em' data='no'>首页</"+options.pageTage+">");
		          		options.page.append("<"+options.pageTage+" class='em' data='no'>上一页</"+options.pageTage+">");
		          	}
	          	}
	          	var i,length=Math.ceil(total/options.size),halfPageCount = Math.ceil(options.showPageCount/2);
	          	if(!total || total==0){options.page.append("<"+options.pageTage+" class='em' data='no'>1</"+options.pageTage+">");}
	          	for (i=1; i <= length; i++) {
	          		if(i == currpage){
	          			options.page.append("<"+options.pageTage+" class='em on' data='"+i+"'>"+i+"</"+options.pageTage+">");
	          		}else if((i<=options.showPageCount && currpage<=halfPageCount) || Math.abs(currpage - i)<halfPageCount || i==currpage+halfPageCount  || (length-currpage<halfPageCount && length-i<options.showPageCount)){
	          			options.page.append("<"+options.pageTage+" class='em' data='"+i+"'>"+i+"</"+options.pageTage+">");
	          		}
	          	}
	          	if(options.upanddown){
		          	if( currpage <= total/options.size){
		          		options.page.append("<"+options.pageTage+" class='em activ' data='"+(currpage+1)+"'>下一页</"+options.pageTage+">");
		          		options.page.append("<"+options.pageTage+" class='em activ' data='"+length+"'>尾页</li>");
		          	}else{
		          		options.page.append("<"+options.pageTage+" class='em' data='no'>下一页</"+options.pageTage+">");
		          		options.page.append("<"+options.pageTage+" class='em' data='no'>尾页</"+options.pageTage+">");
		          	}
		          	//options.page.append("<"+options.pageTage+" class='emno'><span class='totalpage'>"+currpage+"/"+length+"</span> <input type='text' class='currpage' name='currpage'> <input type='button' class='jump' value='跳转'></"+options.pageTage+">");
	          	}
	          	options.after();
          	}
			
			return this.each(function () {
				init($(this));
			 });
		 }
	}
)(jQuery);